<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>C++ static、const 和 static const 类型成员变量声明以及初始化 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../w3cnote/cpp-static-const.html" />
<meta name="keywords" content="C++ static、const 和 static const 类型成员变量声明以及初始化">
<meta name="description" content="const 定义的常量在超出其作用域之后其空间会被释放，而 static 定义的静态常量在函数执行后不会释放其存储空间。  static 表示的是静态的。类的静态成员函数、静态成员变量是和类相关的，而不是和类的具体对象相关的。即使没有具体对象，也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数，只不过它的作用域限于包含它的文件中。  在 C++ 中，static 静态成员变量不能在类的内部初始化。在类的内部只是声明..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<div class="container navigation">
    <div class="row">
        <div class="col nav">
            

                        <ul class="pc-nav" id="note-nav">
                <li><a href="../">首页</a></li>
                <li><a href="../w3cnote">笔记首页</a></li>
                <li><a href="../w3cnote/android-tutorial-intro.html" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" title="ES6 教程">ES6 教程</a></li>
                <li><a href="../w3cnote/ten-sorting-algorithm.html" title="排序算法">排序算法</a></li>
                <li><a href="../w3cnote/hadoop-tutorial.html" title="Hadoop 教程">Hadoop</a></li>
                <li><a href="../w3cnote/zookeeper-tutorial.html" title="Zookeeper 教程">Zookeeper</a></li>
                <li><a href="../w3cnote/verilog-tutorial.html" title="Verilog 教程">Verilog</a></li>
                <li><a href="../w3cnote_genre/code" title="编程技术">编程技术</a></li> 
                <li><a href="../w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
                
                <!--<li><a href="javascript:;" class="runoob-pop">登录</a></li>
                
                
                        <li>
                <a style="font-weight:bold;" href="../linux/linux-tutorial.html#yunserver" target="_blank" onclick="_hmt.push(['_trackEvent', 'aliyun', 'click', 'aliyun'])" title="kkb">云服务器</a>
                </li>
                <li><a href="http://gk.link/a/104mQ" target="_blank" style="font-weight: bold;"onclick="_hmt.push(['_trackEvent', '极客时间', 'click', 'jike'])" title="我的圈子">极客时间</a></li>
            
                
                <li><a target="_blank" href="../shoppinglist" rel="nofollow">知识店铺</a></li> 
        -->
            </ul>
                        
              
            <ul class="mobile-nav">
                <li><a href="../w3cnote">首页</a></li>
                <li><a href="../w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
                <li><a href="../w3cnote/es6-tutorial.html" target="_blank" title="ES6 教程">ES6</a></li>
                <li><a href="../w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
                
                <a href="javascript:void(0)" class="search-reveal">Search</a> 
            </ul>
            
        </div>
    </div>
</div>


<!--  内容  -->
<div class="container main">
	<div class="row">

		<div class="col middle-column big-middle-column">
	 			<div class="article">
			<div class="article-heading">
				<h2>C++ static、const 和 static const 类型成员变量声明以及初始化</h2>				<h3><em>分类</em> <a href="../w3cnote_genre/code" title="编程技术" >编程技术</a> </h3>
			</div>
			<div class="article-body note-body">
				<div class="article-intro">
					
<p>const 定义的常量在超出其作用域之后其空间会被释放，而 static 定义的静态常量在函数执行后不会释放其存储空间。</p>

<p>static 表示的是静态的。类的静态成员函数、静态成员变量是和类相关的，而不是和类的具体对象相关的。即使没有具体对象，也能调用类的静态成员函数和成员变量。一般类的静态函数几乎就是一个全局函数，只不过它的作用域限于包含它的文件中。</p>

<p>在 C++ 中，static 静态成员变量不能在类的内部初始化。在类的内部只是声明，定义必须在类定义体的外部，通常在类的实现文件中初始化，如：<span class="marked">double Account::Rate = 2.25;</span> </p>
<p>
static 关键字只能用于类定义体内部的声明中，定义时不能标示为 static。</p>

<p>在 C++ 中，const 成员变量也不能在类定义处初始化，只能通过构造函数初始化列表进行，并且必须有构造函数。</p>

<p>const 数据成员 只在某个对象生存期内是常量，而对于整个类而言却是可变的。因为类可以创建多个对象，不同的对象其 const 数据成员的值可以不同。所以不能在类的声明中初始化 const 数据成员，因为类的对象没被创建时，编译器不知道 const 数据成员的值是什么。</p>

<p>const 数据成员的初始化只能在类的构造函数的初始化列表中进行。要想建立在整个类中都恒定的常量，应该用类中的枚举常量来实现，或者static cosnt。</p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:</span><span class="hl-identifier">a</span><span class="hl-brackets">(</span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-types">enum</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-identifier">size1</span><span class="hl-code">=</span><span class="hl-number">100</span><span class="hl-code">,</span><span class="hl-identifier">size2</span><span class="hl-code">=</span><span class="hl-number">200</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-reserved">private</span><span class="hl-code">:
    </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">;</span><span class="hl-comment">//只能在构造函数初始化列表中初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">;</span><span class="hl-comment">//在类的实现文件中定义并初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">conststatic</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-code">;</span><span class="hl-comment">//与 static const int c;相同。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code">::</span><span class="hl-identifier">b</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-comment">//static成员变量不能在构造函数初始化列表中初始化，因为它不属于某个对象。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">cosnt</span><span class="hl-code"> </span><span class="hl-identifier">intTest</span><span class="hl-code">::</span><span class="hl-identifier">c</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-comment">//注意：给静态成员变量赋值时，不需要加static修饰符，但要加cosnt。</span></div></div></div>

<p>cosnt 成员函数主要目的是防止成员函数修改对象的内容。即 const 成员函数不能修改成员变量的值，但可以访问成员变量。当方法成员函数时，该函数只能是 const 成员函数。</p>

<p>static 成员函数主要目的是作为类作用域的全局函数。不能访问类的非静态数据成员。类的静态成员函数没有 this 指针，这导致：</p>

<ul><li>
1、不能直接存取类的非静态成员变量，调用非静态成员函数。</li><li>
2、不能被声明为 virtual。</li></ul>
<h3>
关于 static、const、static cosnt、const static 成员的初始化问题</h3>
<p><strong>1、类里的const成员初始化：</strong></p>
<p>在一个类里建立一个 const 时，不能给他初值。</p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">foo</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">foo</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:</span><span class="hl-identifier">i</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-reserved">private</span><span class="hl-code">:
    </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code">=</span><span class="hl-number">100</span><span class="hl-code">;</span><span class="hl-comment">//error!!!</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-comment">//或者通过这样的方式来进行初始化</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">foo</span><span class="hl-code">::</span><span class="hl-identifier">foo</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">:</span><span class="hl-identifier">i</span><span class="hl-brackets">(</span><span class="hl-number">100</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span></div></div></div>
<p><strong>
2、类里的 static 成员初始化：</strong></p>
<p>类中的 static 变量是属于类的，不属于某个对象，它在整个程序的运行过程中只有一个副本，因此不能在定义对象时 对变量进行初始化，就是不能用构造函数进行初始化，其正确的初始化方法是：</p>

<pre>数据类型 类名::静态数据成员名=值；</pre>


<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">foo</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">foo</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">private</span><span class="hl-code">:
</span><span class="hl-identifier">staticint</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-identifier">intfoo</span><span class="hl-code">::</span><span class="hl-identifier">i</span><span class="hl-code">=</span><span class="hl-number">20</span><span class="hl-code">;</span></div></div></div>


<p>这表明：</p>
<ul><li>
1、初始化在类体外进行，而前面不加static，以免与一般静态变量或对象相混淆
</li><li>
2、初始化时不加该成员的访问权限控制符private、public等
</li></ul>
3、初始化时使用作用域运算符来表明它所属的类，因此，静态数据成员是类的成员而不是对象的成员。</li></ul>
<p><strong>3、类里的 static cosnt 和 const static 成员初始化</strong></p>
<p>这两种写法的作用一样，为了便于记忆，在此值说明一种通用的初始化方法：</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">mask1</span><span class="hl-code">;
    </span><span class="hl-identifier">conststatic</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">mask2</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-identifier">constTest</span><span class="hl-code">::</span><span class="hl-identifier">mask1</span><span class="hl-code">=</span><span class="hl-number">0xffff</span><span class="hl-code">;
</span><span class="hl-identifier">constTest</span><span class="hl-code">::</span><span class="hl-identifier">mask2</span><span class="hl-code">=</span><span class="hl-number">0xffff</span><span class="hl-code">;
</span><span class="hl-comment">//它们的初始化没有区别，虽然一个是静态常量一个是常量静态。静态都将存储在全局变量区域，其实最后结果都一样。可能在不同编译器内，不同处理，但最后结果都一样。</span></div></div></div>

<p>完整实例</p>
<div class="example"><h2 class="example">实例</h2><div class="example_code"><div class="hl-main"><span class="hl-prepro">#ifdef</span><span class="hl-code"> </span><span class="hl-identifier">A_H_</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#define</span><span class="hl-code"> </span><span class="hl-identifier">A_H_</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-identifier">usingnamespace</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">;
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code">:
        </span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">staticvoid</span><span class="hl-code"> </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//静态成员函数</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-reserved">private</span><span class="hl-code">:
        </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">aa</span><span class="hl-code">;</span><span class="hl-comment">//静态数据成员的声明</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">staticconst</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">count</span><span class="hl-code">;</span><span class="hl-comment">//常量静态数据成员（可以在构造函数中初始化）</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">bb</span><span class="hl-code">;</span><span class="hl-comment">//常量数据成员</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">::</span><span class="hl-identifier">aa</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-comment">//静态成员的定义+初始化</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">::</span><span class="hl-identifier">count</span><span class="hl-code">=</span><span class="hl-number">25</span><span class="hl-code">;</span><span class="hl-comment">//静态常量成员定义+初始化</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-identifier">A</span><span class="hl-code">::</span><span class="hl-identifier">A</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">:</span><span class="hl-identifier">bb</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-comment">//常量成员的初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">aa</span><span class="hl-code">+=</span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">::</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">count=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">count</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">aa=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">aa</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-prepro">#endif</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">A</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">A</span><span class="hl-code">::</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//通过类访问静态成员函数</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//通过对象访问静态成员函数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span></div></div></div>

<h3>初始化位置</h3>
<p>静态成员不能在类的定义里初始化（除int外）。不能在头文件里初始化。</p>
<p>比如定义了 myclass.h，一般放到 myclass.cpp 里初始化它。</p>
<hr><h2>
C++ 类的静态成员(static)</h2><p>
静态成员的提出是为了解决数据共享的问题。实现共享有许多方法，如：设置全局性的变量或对象是一种方法。但是，全局变量或对象是有局限性的。这一章里，我们主要讲述类的静态成员来实现数据的共享。</p>

<h3>静态数据成员</h3>
<p>在类中，静态成员可以实现多个对象之间的数据共享，并且使用静态数据成员还不会破坏隐藏的原则，即保证了安全性。因此，静态成员是类的所有对象中共享的成员，而不是某个对象的成员。</p>

<p>使用静态数据成员可以节省内存，因为它是所有对象所公有的，因此，对多个对象来说，静态数据成员只存储一处，供所有对象共用。静态数据成员的值对每个对象都是一样，但它的值是可以更新的。只要对静态数据成员的值更新一次，保证所有对象存取更新后的相同的值，这样可以提高时间效率。</p>

<p>静态数据成员的使用方法和注意事项如下：</p>

<p>1、静态数据成员在定义或说明时前面加关键字 static。</p>

<p>2、静态成员初始化与一般数据成员初始化不同。静态数据成员初始化的格式如下：</p>
　<pre>&lt;数据类型&gt;&lt;类名&gt;::&lt;静态数据成员名&gt;=&lt;值&gt;</pre>

<p>这表明：</p>
<ul><li>
(1) 初始化在类体外进行，而前面不加static，以免与一般静态变量或对象相混淆。
</li><li>
(2) 初始化时不加该成员的访问权限控制符private，public等。
</li><li>
(3) 初始化时使用作用域运算符来标明它所属类，因此，静态数据成员是类的成员，而不是对象的成员。
</li></ul>
<p>3、静态数据成员是静态存储的，它是静态生存期，必须对它进行初始化。</p>

<p>4、引用静态数据成员时，采用如下格式：</p>

<pre>&lt;类名&gt;::&lt;静态成员名&gt;</pre>

<p>如果静态数据成员的访问权限允许的话(即public的成员)，可在程序中，按上述格式来引用静态数据成员。</p>

<p>下面举一例子，说明静态数据成员的应用：</p>
<div class="example"><h2 class="example">实例</h2><div class="example_code"><div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> …</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Myclass</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">Myclass</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">GetNumber</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">GetSum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">private</span><span class="hl-code">:
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">, </span><span class="hl-identifier">B</span><span class="hl-code">, </span><span class="hl-identifier">C</span><span class="hl-code">;
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">Sum</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">Myclass</span><span class="hl-code">::</span><span class="hl-identifier">Sum</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
 
</span><span class="hl-identifier">Myclass</span><span class="hl-code">::</span><span class="hl-identifier">Myclass</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">c</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">A</span><span class="hl-code"> = </span><span class="hl-identifier">a</span><span class="hl-code">;
    </span><span class="hl-identifier">B</span><span class="hl-code"> = </span><span class="hl-identifier">b</span><span class="hl-code">;
    </span><span class="hl-identifier">C</span><span class="hl-code"> = </span><span class="hl-identifier">c</span><span class="hl-code">;
    </span><span class="hl-identifier">Sum</span><span class="hl-code"> += </span><span class="hl-identifier">A</span><span class="hl-code">+</span><span class="hl-identifier">B</span><span class="hl-code">+</span><span class="hl-identifier">C</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Myclass</span><span class="hl-code">::</span><span class="hl-identifier">GetNumber</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">Number=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">a</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">,</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">b</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">,</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">c</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
 </span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">Myclass</span><span class="hl-code">::</span><span class="hl-identifier">GetSum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">Sum=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">sum</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
 </span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">Myclass</span><span class="hl-code"> </span><span class="hl-identifier">M</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">7</span><span class="hl-code">, </span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-identifier">N</span><span class="hl-brackets">(</span><span class="hl-number">14</span><span class="hl-code">, </span><span class="hl-number">9</span><span class="hl-code">, </span><span class="hl-number">11</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">M</span><span class="hl-code">.</span><span class="hl-identifier">GetNumber</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">N</span><span class="hl-code">.</span><span class="hl-identifier">GetNumber</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">M</span><span class="hl-code">.</span><span class="hl-identifier">GetSum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">N</span><span class="hl-code">.</span><span class="hl-identifier">GetSum</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>
<p>从输出结果可以看到Sum的值对M对象和对N对象都是相等的。这是因为在初始化M对象时，将M对象的三个int型数据成员的值求和后赋给了Sum，于是Sum保存了该值。在初始化N对象时，对将N对象的三个int型数据成员的值求和后又加到Sum已有的值上，于是Sum将保存另后的值。所以，不论是通过对象M还是通过对象N来引用的值都是一样的，即为54。</p>

<h2>静态成员函数</h2>
<p>静态成员函数和静态数据成员一样，它们都属于类的静态成员，它们都不是对象成员。因此，对静态成员的引用不需要用对象名。</p>

<p>在静态成员函数的实现中不能直接引用类中说明的非静态成员，可以引用类中说明的静态成员。如果静态成员函数中要引用非静态成员时，可通过对象来引用。下面通过例子来说明这一点。</p>
<div class="example"><h2 class="example">实例</h2><div class="example_code"><div class="hl-main"><span class="hl-prepro">#include</span><span class="hl-prepro"> …</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">M</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">M</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">A</span><span class="hl-code">=</span><span class="hl-identifier">a</span><span class="hl-code">;
        </span><span class="hl-identifier">B</span><span class="hl-code">+=</span><span class="hl-identifier">a</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">f1</span><span class="hl-brackets">(</span><span class="hl-identifier">M</span><span class="hl-code"> </span><span class="hl-identifier">m</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">private</span><span class="hl-code">:
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">A</span><span class="hl-code">;
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">B</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">M</span><span class="hl-code">::</span><span class="hl-identifier">f1</span><span class="hl-brackets">(</span><span class="hl-identifier">M</span><span class="hl-code"> </span><span class="hl-identifier">m</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">A=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">m</span><span class="hl-code">.</span><span class="hl-identifier">a</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">B=</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">b</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">M</span><span class="hl-code">::</span><span class="hl-identifier">B</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;
 
</span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">M</span><span class="hl-code"> </span><span class="hl-identifier">P</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-identifier">Q</span><span class="hl-brackets">(</span><span class="hl-number">10</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">M</span><span class="hl-code">::</span><span class="hl-identifier">f1</span><span class="hl-brackets">(</span><span class="hl-identifier">P</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//调用时不用对象名</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">M</span><span class="hl-code">::</span><span class="hl-identifier">f1</span><span class="hl-brackets">(</span><span class="hl-identifier">Q</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>


<p>读者可以自行分析其结果。从中可看出，调用静态成员函数使用如下格式：</p>

　　<pre>&lt;类名&gt;::&lt;静态成员函数名&gt;(&lt;参数表&gt;);</pre>
<hr>
<h2>C++ 中各种类型的成员变量的初始化方法</h2>
<p>C++各种不同类型成员根据是否static、时候const类型的初始化方法不尽相同，java的语法就没有这么复杂，怪的得那么多人都跑去学Java了。以前面试时被人问到这个问题回答不出来，写代码时也经常搞乱了，这里翻了下书，总结一下。</p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-comment">//-----------------Test.h----------------------------</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-prepro">#pragma</span><span class="hl-code"> </span><span class="hl-identifier">once</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">private</span><span class="hl-code"> :
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">var1</span><span class="hl-code">;
    </span><span class="hl-comment">// int var11= 4; 错误的初始化方法</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">var2</span><span class="hl-code"> ;
    </span><span class="hl-comment">// const int var22 =22222; 错误的初始化方法</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">var3</span><span class="hl-code">;
    </span><span class="hl-comment">// static int var3333=33333; 错误，只有静态常量int成员才能直接赋值来初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">var4</span><span class="hl-code">=</span><span class="hl-number">4444</span><span class="hl-code">; </span><span class="hl-comment">//正确，静态常量成员可以直接初始化    </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">var44</span><span class="hl-code">;
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-types">void</span><span class="hl-brackets">)</span><span class="hl-code">;
    ~</span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-types">void</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-comment">//--------------------Test.cpp-----------------------------------</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&quot;</span><span class="hl-string">.</span><span class="hl-special">\</span><span class="hl-string">test.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro"></span><span class="hl-code">
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-code">::</span><span class="hl-identifier">var3</span><span class="hl-code"> = </span><span class="hl-number">3333333</span><span class="hl-code">; </span><span class="hl-comment">//静态成员的正确的初始化方法</span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-comment">// int Test::var1 = 11111;; 错误静态成员才能初始化</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">// int Test::var2 = 22222; 错误</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">// int Test::var44 = 44444; // 错误的方法，提示重定义</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-identifier">Test</span><span class="hl-code">::</span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-types">void</span><span class="hl-brackets">)</span><span class="hl-code">：</span><span class="hl-identifier">var1</span><span class="hl-brackets">(</span><span class="hl-number">11111</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-identifier">var2</span><span class="hl-brackets">(</span><span class="hl-number">22222</span><span class="hl-brackets">)</span><span class="hl-code">正确的初始化方法</span><span class="hl-comment">//var3(33333)不能在这里初始化</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">var1</span><span class="hl-code"> =</span><span class="hl-number">11111</span><span class="hl-code">; </span><span class="hl-comment">//正确, 普通变量也可以在这里初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//var2 = 222222; 错误，因为常量不能赋值，只能在 "constructor initializer （构造函数的初始化列表）" 那里初始化</span><span class="hl-comment"></span><span class="hl-code">
          
    </span><span class="hl-identifier">var3</span><span class="hl-code"> =</span><span class="hl-number">44444</span><span class="hl-code">; </span><span class="hl-comment">//这个赋值是正确的，不过因为所有对象一个静态成员，所以会影响到其他的，这不能叫做初始化了吧</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-identifier">Test</span><span class="hl-code">::~</span><span class="hl-identifier">Test</span><span class="hl-brackets">(</span><span class="hl-types">void</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-comment">//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</span></div></div></div>

<p>有些成员变量的数据类型比较特别，它们的初始化方式也和普通数据类型的成员变量有所不同。这些特殊的类型的成员变量包括：</p>
<ul><li>
    a. 常量型成员变量</li><li>
    b. 引用型成员变量</li><li>
    c. 静态成员变量</li><li>
    d. 整型静态常量成员变量</li><li>
    e. 非整型静态常量成员变量</li></ul>
 <p>对于常量型成员变量和引用型成员变量的初始化，必须通过构造函数初始化列表的方式进行。在构造函数体内给常量型成员变量和引用型成员变量赋值的方式是行不通的。</p>

   <p> 静态成员变量的初始化也颇有点特别。</p>
   <p>参考下面的代码以及其中注释：</p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-comment">// Initialization of Special Data Member</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-prepro">#include</span><span class="hl-prepro"> </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro"></span><span class="hl-code">
</span><span class="hl-reserved">using</span><span class="hl-code"> </span><span class="hl-types">namespace</span><span class="hl-code"> </span><span class="hl-identifier">std</span><span class="hl-code">;     
 
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">BClass</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code">:
    </span><span class="hl-identifier">BClass</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> : </span><span class="hl-identifier">i</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">,</span><span class="hl-identifier">ci</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">, </span><span class="hl-identifier">ri</span><span class="hl-brackets">(</span><span class="hl-identifier">i</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-comment">// 对于常量型成员变量和引用型成员变量，必须通过    </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">                </span><span class="hl-comment">// 参数化列表的方式进行初始化。在构造函数体内进行赋值的方式，是行不通的。</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code"> 
 
    </span><span class="hl-identifier">voidprint_values</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">i =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">ci =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">ci</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">ri =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">ri</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">si =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">si</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">csi =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">csi</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">csi2 =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">csi2</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
        </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt; </span><span class="hl-quotes">&quot;</span><span class="hl-string">csd =</span><span class="hl-special">\</span><span class="hl-string">t</span><span class="hl-quotes">&quot;</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">csd</span><span class="hl-code"> &lt;&lt; </span><span class="hl-identifier">endl</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
 
</span><span class="hl-reserved">private</span><span class="hl-code">:
    </span><span class="hl-identifier">inti</span><span class="hl-code">; </span><span class="hl-comment">// 普通成员变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">constint</span><span class="hl-code"> </span><span class="hl-identifier">ci</span><span class="hl-code">; </span><span class="hl-comment">// 常量成员变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code">&amp;</span><span class="hl-identifier">ri</span><span class="hl-code">; </span><span class="hl-comment">// 引用成员变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">staticint</span><span class="hl-code"> </span><span class="hl-identifier">si</span><span class="hl-code">; </span><span class="hl-comment">// 静态成员变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//staticint si2 = 100; // error: 只有静态常量成员变量，才可以这样初始化</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">staticconst</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">csi</span><span class="hl-code">; </span><span class="hl-comment">// 静态常量成员变量</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">staticconst</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">csi2</span><span class="hl-code"> = </span><span class="hl-number">100</span><span class="hl-code">; </span><span class="hl-comment">// 静态常量成员变量的初始化(Integral type) (1)</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">staticconst</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">csd</span><span class="hl-code">; </span><span class="hl-comment">// 静态常量成员变量(non-Integral type)</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-comment">//staticconst double csd2 = 99.9; // error: 只有静态常量整型数据成员才可以在类中初始化</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;
</span><span class="hl-comment">// 静态成员变量的初始化(Integral type)</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">BClass</span><span class="hl-code">::</span><span class="hl-identifier">si</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">;
</span><span class="hl-comment">// 静态常量成员变量的初始化(Integral type)</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">BClass</span><span class="hl-code">::</span><span class="hl-identifier">csi</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
</span><span class="hl-comment">// 静态常量成员变量的初始化(non-Integral type)</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">BClass</span><span class="hl-code">::</span><span class="hl-identifier">csd</span><span class="hl-code"> = </span><span class="hl-number">99</span><span class="hl-number">.9</span><span class="hl-code">;
 
</span><span class="hl-comment">// 在初始化(1)中的csi2时，根据Stanley B. Lippman的说法下面这行是必须的。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-comment">// 但在VC2003中如果有下面一行将会产生错误，而在VC2005中，下面这行则可有可无，这个和编译器有关。</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">BClass</span><span class="hl-code">::</span><span class="hl-identifier">csi2</span><span class="hl-code">;
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-types">void</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">BClassb_class</span><span class="hl-code">;
    </span><span class="hl-identifier">b_class</span><span class="hl-code">.</span><span class="hl-identifier">print_values</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; 
    </span><span class="hl-identifier">return0</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></div></div></div>

<hr>
<h2>c++ 静态成员小结,c++,static</h2>

类中的静态成员真是个让人爱恨交加的特性。我曾经在面试时，被主考官抓住这个问题一阵穷追猛打，直把我问的面红耳赤，败下阵来。所以回来之<p>后，我痛定思痛，决定好好总结一下静态类成员的知识点，以便自己在以后面试中，在此类问题上不在被动。 </p>
<p>静态类成员包括静态数据成员和静态函数成员两部分。 </p>

<h3>静态数据成员</h3>
<p>类体中的数据成员的声明前加上static关键字，该数据成员就成为了该类的静态数据成员。和其他数据成员一样，静态数据成员也遵守public/protected/private访问规则。同时，静态数据成员还具有以下特点： </p>

<p><strong>1.静态数据成员的定义。 </strong></p>
<p>静态数据成员实际上是类域中的全局变量。所以，静态数据成员的定义(初始化)不应该被放在头文件中。 （声明vs.定义（初始化））
其定义方式与全局变量相同。举例如下： </p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-identifier">xxx</span><span class="hl-code">.</span><span class="hl-identifier">h</span><span class="hl-code">文件 
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
</span><span class="hl-reserved">private</span><span class="hl-code">: 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_i</span><span class="hl-code">;</span><span class="hl-comment">//声明，标准c++支持有序类型在类体中初始化,但vc6不支持。 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">; 
 
</span><span class="hl-identifier">xxx</span><span class="hl-code">.</span><span class="hl-identifier">cpp</span><span class="hl-code">文件 
</span><span class="hl-types">const</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-code">::</span><span class="hl-identifier">_i</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">;</span><span class="hl-comment">//定义(初始化)时不受private和protected访问限制.</span></div></div></div>
<p><strong>注：</strong>不要试图在头文件中定义(初始化)静态数据成员。在大多数的情况下，这样做会引起重复定义这样的错误。即使加上<span class="marked">#ifndef #define #endif</span> 或者 <span class="marked">#pragma once</span> 也不行。 </p>

<p>2.静态数据成员被类的所有对象所共享，包括该类派生类的对象。即派生类对象与基类对象共享基类的静态数据成员。举例如下： </p>


<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
</span><span class="hl-reserved">public</span><span class="hl-code"> : 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_num</span><span class="hl-code">;</span><span class="hl-comment">//声明 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-code">::</span><span class="hl-identifier">_num</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span><span class="hl-comment">//静态数据成员的真正定义 </span><span class="hl-comment"></span><span class="hl-code">
 
</span><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">derived</span><span class="hl-code">:</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> </span><span class="hl-brackets">}</span><span class="hl-code">; 
 
</span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> 
</span><span class="hl-brackets">{</span><span class="hl-code"> 
    </span><span class="hl-identifier">base</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">; 
    </span><span class="hl-identifier">derived</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">; 
    </span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">_num</span><span class="hl-code">++; 
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">base class static data number_num is</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">a</span><span class="hl-code">.</span><span class="hl-identifier">_num</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">; 
    </span><span class="hl-identifier">b</span><span class="hl-code">.</span><span class="hl-identifier">_num</span><span class="hl-code">++; 
    </span><span class="hl-identifier">cout</span><span class="hl-code">&lt;&lt;</span><span class="hl-quotes">&quot;</span><span class="hl-string">derived class static datanumber _num is</span><span class="hl-quotes">&quot;</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">b</span><span class="hl-code">.</span><span class="hl-identifier">_num</span><span class="hl-code">&lt;&lt;</span><span class="hl-identifier">endl</span><span class="hl-code">; 
</span><span class="hl-brackets">}</span><span class="hl-code"> 
</span><span class="hl-comment">// 结果为1,2;可见派生类与基类共用一个静态数据成员。</span></div></div></div>

<p>3.静态数据成员可以成为成员函数的可选参数，而普通数据成员则不可以。举例如下： </p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
</span><span class="hl-reserved">public</span><span class="hl-code"> : 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_staticVar</span><span class="hl-code">; 
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_var</span><span class="hl-code">; 
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">foo1</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code">=</span><span class="hl-identifier">_staticVar</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//正确,_staticVar为静态数据成员 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">foo2</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code">=</span><span class="hl-identifier">_var</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//错误,_var为普通数据成员 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;</span></div></div></div>
<p>4.静态数据成员的类型可以是所属类的类型，而普通数据成员则不可以。普通数据成员的只能声明为所属类类型的指针或引用。举例如下： </p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
</span><span class="hl-reserved">public</span><span class="hl-code"> : 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-identifier">base_object1</span><span class="hl-code">;</span><span class="hl-comment">//正确，静态数据成员 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">base</span><span class="hl-code"> </span><span class="hl-identifier">_object2</span><span class="hl-code">;</span><span class="hl-comment">//错误 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">base</span><span class="hl-code"> *</span><span class="hl-identifier">pObject</span><span class="hl-code">;</span><span class="hl-comment">//正确，指针 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">base</span><span class="hl-code"> &amp;</span><span class="hl-identifier">mObject</span><span class="hl-code">;</span><span class="hl-comment">//正确，引用 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;</span></div></div></div>


<p>5.这个特性，我不知道是属于标准c++中的特性，还是vc6自己的特性。 </p>
<p>静态数据成员的值在const成员函数中可以被合法的改变。举例如下： </p>

<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
</span><span class="hl-reserved">public</span><span class="hl-code">: 
    </span><span class="hl-identifier">base</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-identifier">_i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;
        </span><span class="hl-identifier">_val</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code"> 
 
    </span><span class="hl-types">mutable</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_i</span><span class="hl-code">; 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_staticVal</span><span class="hl-code">; 
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">_val</span><span class="hl-code">; 
    </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">test</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-brackets">{</span><span class="hl-comment">//const 成员函数 </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">_i</span><span class="hl-code">++;</span><span class="hl-comment">//正确，mutable数据成员 </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">_staticVal</span><span class="hl-code">++;</span><span class="hl-comment">//正确，static数据成员 </span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">_val</span><span class="hl-code">++;</span><span class="hl-comment">//错误 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-brackets">}</span><span class="hl-code"> 
</span><span class="hl-brackets">}</span><span class="hl-code">; 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-code">::</span><span class="hl-identifier">_staticVal</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">;</span></div></div></div>


<h2>静态成员函数 </h2>
<p>静态成员函数没有什么太多好讲的。 </p>

<p>1.静态成员函数的地址可用普通函数指针储存，而普通成员函数地址需要用类成员函数指针来储存。举例如下： </p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">func1</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; 
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">func2</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; 
</span><span class="hl-brackets">}</span><span class="hl-code">; 
 
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-code">*</span><span class="hl-identifier">pf1</span><span class="hl-brackets">)</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">=&amp;</span><span class="hl-identifier">base</span><span class="hl-code">::</span><span class="hl-identifier">func1</span><span class="hl-code">;</span><span class="hl-comment">//普通的函数指针 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">base</span><span class="hl-code">::*</span><span class="hl-identifier">pf2</span><span class="hl-brackets">)</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">=&amp;</span><span class="hl-identifier">base</span><span class="hl-code">::</span><span class="hl-identifier">func2</span><span class="hl-code">;</span><span class="hl-comment">//成员函数指针</span></div></div></div>

<p>2.静态成员函数不可以调用类的非静态成员。因为静态成员函数不含this指针。 </p>

<p>3.静态成员函数不可以同时声明为 virtual、const、volatile函数。举例如下：</p>
<div class="example"><div class="example_code"><div class="hl-main"><span class="hl-types">class</span><span class="hl-code"> </span><span class="hl-identifier">base</span><span class="hl-brackets">{</span><span class="hl-code"> 
    </span><span class="hl-types">virtual</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">func1</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;</span><span class="hl-comment">//错误 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">func2</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">const</span><span class="hl-code">;</span><span class="hl-comment">//错误 </span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">func3</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-types">volatile</span><span class="hl-code">;</span><span class="hl-comment">//错误 </span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">;</span></div></div></div> 
<p>最后要说的一点是，静态成员是可以独立访问的，也就是说，无须创建任何对象实例就可以访问。</p>
<blockquote><p>作者：SDMrFeng </p>
<p>原文：https://blog.csdn.net/tobefxz/article/details/14109697 </p></blockquote>

				</div>
			</div>
			<div class="previous-next-links">
			<div class="previous-design-link">← <a href="../w3cnote/pycharm-import-python-package.html" rel="prev"> Pycharm 导入 Python 包、模块</a> </div>
			<div class="next-design-link"><a href="../w3cnote/linux-common-command-2.html" rel="next"> Linux 常用命令学习</a> →</div>
			</div>
			<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style=" padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='21097' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  21097;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 21097, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":21097},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
			<div class="article-heading-ad" id="w3cnote-ad728">
			<script async src=""></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="horizontal"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
			</div>
			<style>
@media screen and (max-width: 768px) {
	#w3cnote-ad728 {
		display: none;
	}
}
p.note-author {
    border-bottom: 1px solid #ddd;
    font-size: 18px;
    font-weight: bold;
    color: #78a15a;
    padding-bottom: 2px;
    margin-bottom: 4px;
}
</style>
<script>
var aid = 21097;
</script>
	</div>
		
	</div>
	<div class="listcol last right-column">




<!--
	<div class="tab tab-light-blue"> 订阅</div>
	<div class="sidebar-box">
		<div class="socialicons">
			<a href="../feed" class="rss">RSS 订阅</a>
		
			<form action="//list.qq.com/cgi-bin/qf_compose_send" method="post">
			<input type="hidden" value="qf_booked_feedback" name="t">
			<input type="hidden" value="4b67b6b6c1f5e792559940cab4aebb8f1126fba880bff1a8" name="id">
			<input class="placeholder" id="feed_email" name="to" value="输入邮箱 订阅笔记" autocomplete="off">
			<input type="submit" value="订阅" class="btn btn-primary">
			</form>
		
		</div>
 
	</div>
-->	



<!--
	<div class="sidebar-box cate-list">
	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">笔记列表</a>
		</div>

 

</div>
-->

	 <div class="sidebar-box cate-list">
		 		

	 	<div class="sidebar-box recommend-here list-link">
			<a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;">教程列表</a>
		</div>
		
		<div class="cate-items"> 
				<a href="../ado">ADO 教程</a>
	<a href="../ajax">Ajax 教程</a>
	<a href="../android">Android 教程</a>
	<a href="../angularjs2">Angular2 教程</a>
	<a href="../angularjs">AngularJS 教程</a>
	<a href="../appml">AppML 教程</a>
	<a href="../asp">ASP 教程</a>
	<a href="../aspnet">ASP.NET 教程</a>
	<a href="../bootstrap">Bootstrap 教程</a>
	<a href="../bootstrap4">Bootstrap4 教程</a>
	<a href="../bootstrap5">Bootstrap5 教程</a>
	<a href="../cprogramming">C 教程</a>
	<a href="../csharp">C# 教程</a>
	<a href="../cplusplus">C++ 教程</a>
	<a href="../chartjs">Chart.js 教程</a>
	<a href="../cssref">CSS 参考手册</a>
	<a href="../css">CSS 教程</a>
	<a href="../css3">CSS3 教程</a>
	<a href="../django">Django 教程</a>
	<a href="../docker">Docker 教程</a>
	<a href="../dtd">DTD 教程</a>
	<a href="../echarts">ECharts 教程</a>
	<a href="../eclipse">Eclipse 教程</a>
	<a href="../firebug">Firebug 教程</a>
	<a href="../font-awesome">Font Awesome 图标</a>
	<a href="../foundation">Foundation 教程</a>
	<a href="../git">Git 教程</a>
	<a href="../go">Go 语言教程</a>
	<a href="../googleapi">Google 地图 API 教程</a>
	<a href="../highcharts">Highcharts 教程</a>
	<a href="../htmldom">HTML DOM 教程</a>
	<a href="../tags">HTML 参考手册</a>
	<a href="../charsets">HTML 字符集</a>
	<a href="../html">HTML 教程</a>
	<a href="../http">HTTP 教程</a>
	<a href="../ionic">ionic 教程</a>
	<a href="../ios">iOS 教程</a>
	<a href="../java">Java 教程</a>
	<a href="../jsref">JavaScript 参考手册</a>
	<a href="../js">Javascript 教程</a>
	<a href="../jeasyui">jQuery EasyUI 教程</a>
	<a href="../jquerymobile">jQuery Mobile 教程</a>
	<a href="../jqueryui">jQuery UI 教程</a>
	<a href="../jquery">jQuery 教程</a>
	<a href="../json">JSON 教程</a>
	<a href="../jsp">JSP 教程</a>
	<a href="../julia">Julia 教程</a>
	<a href="../kotlin">Kotlin 教程</a>
	<a href="../linux">Linux 教程</a>
	<a href="../lua">Lua 教程</a>
	<a href="../markdown">Markdown 教程</a>
	<a href="../matplotlib">Matplotlib 教程</a>
	<a href="../maven">Maven 教程</a>
	<a href="../memcached">Memcached 教程</a>
	<a href="../mongodb">MongoDB 教程</a>
	<a href="../mysql">MySQL 教程</a>
	<a href="../nodejs">Node.js 教程</a>
	<a href="../numpy">NumPy 教程</a>
	<a href="../pandas">Pandas 教程</a>
	<a href="../perl">Perl 教程</a>
	<a href="../php">PHP 教程</a>
	<a href="../postgresql">PostgreSQL 教程</a>
	<a href="../python3">Python 3 教程</a>
	<a href="../python">Python 基础教程</a>
	<a href="../r">R 教程</a>
	<a href="../rdf">RDF 教程</a>
	<a href="../react">React 教程</a>
	<a href="../redis">Redis 教程</a>
	<a href="../rss">RSS 教程</a>
	<a href="../ruby">Ruby 教程</a>
	<a href="../rust">Rust 教程</a>
	<a href="../sass">Sass 教程</a>
	<a href="../scala">Scala 教程</a>
	<a href="../scipy">SciPy 教程</a>
	<a href="../servlet">Servlet 教程</a>
	<a href="../soap">SOAP 教程</a>
	<a href="../sql">SQL 教程</a>
	<a href="../sqlite">SQLite 教程</a>
	<a href="../svg">SVG 教程</a>
	<a href="../svn">SVN 教程</a>
	<a href="../swift">Swift 教程</a>
	<a href="../tcpip">TCP/IP 教程</a>
	<a href="../typescript">TypeScript 教程</a>
	<a href="../vbscript">VBScript 教程</a>
	<a href="../vue2">Vue.js 教程</a>
	<a href="../vue3">Vue3 教程</a>
	<a href="../w3c">W3C 教程</a>
	<a href="../webservices">Web Service 教程</a>
	<a href="../wsdl">WSDL 教程</a>
	<a href="../xlink">XLink 教程</a>
	<a href="../dom">XML DOM 教程</a>
	<a href="../schema">XML Schema 教程</a>
	<a href="../xml">XML 教程</a>
	<a href="../xpath">XPath 教程</a>
	<a href="../xquery">XQuery 教程</a>
	<a href="../xslfo">XSLFO 教程</a>
	<a href="../xsl">XSLT 教程</a>
	<a href="../data-structures">数据结构</a>
	<a href="../regexp">正则表达式</a>
	<a href="../quiz">测验</a>
	<a href="../browsers">浏览器</a>
	<a href="../quality">网站品质</a>
	<a href="../web">网站建设指南</a>
	<a href="../hosting">网站服务器教程</a>
	<a href="../design-pattern">设计模式</a>
			
		</div> 
		 	 </div>
</div>
	</div>
</div>


<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>